package com.juzipi.demo.service;


@Service("userDetailsService")

public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    private UsersMapper usersMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)){
            throw new UsernameNotFoundException("Bad_Request!");
        }
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        //查询用户
        Users users = usersMapper.selectOne(wrapper);

        //判断
        if (StringUtils.isBlank(users.getUsername()) && StringUtils.isBlank(users.getPassword())){
            throw new UsernameNotFoundException("NOT_FOUND!");
        }

        //在这里把返回的user对象设置权限
        List<GrantedAuthority> role = AuthorityUtils.commaSeparatedStringToAuthorityList("admins,manager");
        //查询数据库返回users对象，得到用户名和密码
        return new User(username,new BCryptPasswordEncoder().encode(users.getPassword()),role);
    }



}
